package adapletplugin.commands;

import objects.MappingFile;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

import adapletplugin.Activator;
import adapletplugin.dialogs.NewMapDialog;
import adapletplugin.editors.MapFileEditor;
import adapletplugin.editors.input.MapFileEditorInput;

public class NewProject extends AbstractHandler
{

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException
	{
		MappingFile mapFile = new MappingFile();

		Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
		NewMapDialog dialog = new NewMapDialog(shell);

		dialog.create();

		dialog.open();

		if (dialog.getReturnCode() == Window.OK)
		{
			mapFile.setMapFileName(dialog.getMapFileName());
			Activator.getDefault().setCurrentProject(mapFile);

			try
			{
				PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
						.openEditor(new MapFileEditorInput(mapFile), MapFileEditor.ID);
			} catch (PartInitException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return null;
	}

}
